﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.collections {
    import reprise.ui.*;
    import flash.events.*;
    import flash.display.*;
    import org.robotlegs.core.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.vo.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.*;
    import reprise.tweens.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import com.robertpenner.easing.*;

    public class CollectionStatusMediator extends Mediator implements IMediator {

		[Inject]
        public var view:CollectionStatusDisplay;
		[Inject]
        public var language:LanguageModel;
		[Inject]
        public var collectableModel:CollectableModel;
		[Inject]
        public var popupModel:PopupModel;
        private var closeTimer:Timer;
        private var hasCollection:Array;
        private var _type:String;
        private var _collectionVO:CollectableVO;

        private static const CLOSING_IN:int = 3000;

        override public function onRegister():void{
            eventMap.mapListener(eventDispatcher, EventConstants.CREATE_POPUP, this.close);
            eventMap.mapListener(eventDispatcher, EventConstants.VISIT_FRIEND, this.close);
            eventMap.mapListener(eventDispatcher, EventConstants.UPDATE_COLLECTION_STATUS, this.onUpdate);
            this.view.addEventListener(MouseEvent.CLICK, this.onClick);
        }
        private function onUpdate(_arg1:InfoEvent):void{
            var _local5:Boolean;
            if (this.popupModel.isPopupOpen()){
                return;
            };
            this.view.reset();
            this._type = _arg1.id;
            this._collectionVO = (_arg1.info.collectable as CollectableVO);
            var _local2:int = _arg1.info.updatedIndex;
            var _local3:String = this.language.getTextByKey(((("collections_" + this._type) + "_") + this._collectionVO.rewardPet));
            this.view.setTitle(_local3);
            this.initHasCollection();
            var _local4:int;
            while (_local4 <= 3) {
                _local5 = (_local2 == _local4);
                this.createCollectableItem(_local4, _local5);
                _local4++;
            };
            this.view.open();
            if (((this.closeTimer) && (this.closeTimer.running))){
                this.closeTimer.stop();
            };
            this.closeTimer = new Timer(CLOSING_IN, 1);
            this.closeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, this.onClose);
            this.closeTimer.start();
        }
        private function initHasCollection():void{
            var _local1:CollectableVO = this.collectableModel.getCollectableByType(this._type);
            this.hasCollection = _local1.collected;
            var _local2:int = this.collectableModel.getCollectableByType(this._collectionVO.rewardPet);
            this.hasCollection.push(_local2);
        }
        private function createCollectableItem(_arg1:int, _arg2:Boolean):void{
            var _local6:UIComponent;
            var _local7:String;
            var _local8:String;
            var _local3:int = this.hasCollection[_arg1];
            var _local4:int = this._collectionVO.requires[_arg1];
            var _local5 = (_local3 >= _local4);
            _local6 = RepriseUtils.addComponent(this.view.content, "collectedItem");
            if (_arg1 < 3){
                _local7 = ((("RC_Collectable_" + this._collectionVO.type) + "_") + (_arg1 + 1));
            } else {
                _local7 = ("RC_Collectable_" + this._collectionVO.rewardPet);
            };
            this.addCollectableIcon(_local6, _local7, _arg2);
            if (_local5){
                this.addSolvedIcon(_local6);
            } else {
                _local8 = (_arg2) ? "collectableLabelHighlight" : "collectableLabel";
                this.createCollectableLabel(_local6, _local8, _local3, _local4);
            };
        }
        private function addCollectableIcon(_arg1:UIComponent, _arg2:String, _arg3:Boolean):void{
            var target = _arg1;
            var key = _arg2;
            var isUpdated = _arg3;
            var iconClip:MovieClip = AssetsLibrary.getMovieClip(key);
            var icon:UIComponent = RepriseUtils.addClipComponent(target, "collectableItemContainer", iconClip);
            RepriseUtils.validateComponent(icon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, 0, 0);
                if (isUpdated){
                    highlightIcon(_arg1);
                };
            });
        }
        private function highlightIcon(_arg1:ClipComponent):void{
            var _local2:MovieClip = AssetsLibrary.getMovieClip("RC_HighlightStars");
            _local2.scaleX = (_local2.scaleY = 1.5);
            RepriseUtils.addClipComponent(_arg1, "", _local2);
            var _local3:SimpleTween = new SimpleTween(400, 500);
            _local3.addTweenProperty(_arg1, "scaleX", _arg1.scaleX, 0.8, Quad.easeInOut);
            _local3.addTweenProperty(_arg1, "scaleY", _arg1.scaleY, 0.8, Quad.easeInOut);
            _local3.execute();
        }
        private function addSolvedIcon(_arg1:UIComponent):void{
            var target = _arg1;
            var solvedClip:MovieClip = AssetsLibrary.getMovieClip("SolvedIcon");
            var solvedIcon:UIComponent = RepriseUtils.addClipComponent(target, "collectionSolvedIcon", solvedClip);
            RepriseUtils.validateComponent(solvedIcon, function (_arg1:ClipComponent):void{
                Displays.correctScaling(_arg1, _arg1.clip, _arg1.width, _arg1.height, -1, -1);
            });
        }
        private function createCollectableLabel(_arg1:UIComponent, _arg2:String, _arg3:int, _arg4:int):void{
            var _local5:String = this.language.getTextByKey("global_of_new", {has:_arg3, needed:_arg4});
            RepriseUtils.addLabel(_arg1, _arg2, _local5);
        }
        private function onClose(_arg1:Event=null):void{
            if (this.closeTimer){
                this.closeTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, this.onClose);
            };
            this.view.close();
        }
        private function onClick(_arg1:MouseEvent):void{
            this.view.close(false);
            dispatch(new InfoEvent(EventConstants.CREATE_POPUP, GameConstants.COLLECTION_POPUP, this._type));
        }
        private function close(_arg1:Event=null):void{
            this.view.close(false);
        }

    }
}//package net.wooga.pets.views.collections 
